
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "errlog.h"
#include "glovar.h"
#include "grid3d.h"
#include "f3io.h"

#include "soah_common.h"
#include "common.h"

char log_fname[FILENAME_MAX] = "\0";
int quiet = FALSE;
int dump_bin = 0;

static int parse_input_file(const char *fname);

void parse_cmd_line(int argc, char *argv[])
{
    int i, G;

    PrintLog("++ parsing command line:\n");
    for(i = 1; i < argc; i++)
    {
        if(argv[i][0] != '-')
        {
            if(!parse_input_file(argv[i]))
                warning_msg("cannot parse file %s\n", argv[i]);
        }
        else
        {
            switch(argv[i][1])
            {
                case 'g':
                    if(grid != NULL)
                        error_msg("Multiple grids.\n");
                    if(argv[i][2] == '=')
                    {
                        double X0, X1, Y0, Y1, Z0, Z1;
                        int NX, NY, NZ;
                        if(i + 9 >= argc)
                            error_msg("Invalid use of -g\n");
                        i++;
                        X0 = atof(argv[i]);
                        i++;
                        X1 = atof(argv[i]);
                        i++;
                        NX = atoi(argv[i]);
                        i++;
                        Y0 = atof(argv[i]);
                        i++;
                        Y1 = atof(argv[i]);
                        i++;
                        NY = atoi(argv[i]);
                        i++;
                        Z0 = atof(argv[i]);
                        i++;
                        Z1 = atof(argv[i]);
                        i++;
                        NZ = atoi(argv[i]);
                        grid = GenerateGrid1(X0, X1, NX, Y0, Y1, NY, Z0, Z1, NZ);
                    }
                    else
                        grid = GenerateGrid(atoi(&argv[i][2]));
                    break;
                case 'G':
                {
                    if(grid != NULL)
                        error_msg("Multiple grids.\n");
                    char *fname = argv[i] + 2;
                    char *ext = strrchr(fname, '.');
                    PrintLog("\tLoading grid from file %s\n", fname);
                    if(ext == NULL || (strcmp(ext, ".g3b") != 0 && strcmp(ext, ".g3t") != 0))
                    {
                        grid = (grid3d_t *) calloc(1, sizeof (grid3d_t));
                        if(grid == NULL)
                            error_msg("%s: cannot allocate memory.\n", __func__);
                        load_grid(fname);
                    }
                    else if(strcmp(ext, "g3b") == 0)
                        grid = LoadGridBin(fname);
                    else
                        grid = LoadGrid(fname);
                    break;
                }
                case 't':
                case 'T':
                    if(i + 2 >= argc)
                        error_msg("improper use of -t option\n");
                    SetTimeParams(0.0, atof(argv[i + 2]), atof(argv[i + 1]));
                    i += 2;
                    break;
                case 'q':
                    quiet = TRUE;
                    PrintLog("Quiet mode is ON.\n");
                    break;
                case 'b':
                    if(!isdigit(argv[i][2]))
                        dump_bin = 1;
                    else
                        dump_bin = atoi(&argv[i][2]);
                    PrintLog("Dumping binary data every %3d steps.\n", dump_bin);
                    break;
                case 'o':
                {
                    int q = atoi(&argv[i][2]);
                    flags.time_order = q % 4;
                    PrintLog("Setting flags.time_order = %d\n", flags.time_order);
                    break;
                }
                case 'c':
                    if(argv[i][2] == '-')
                    {
                        flags.convergence = FALSE;
                        PrintLog("Convergence mode is OFF.\n");
                    }
                    else
                    {
                        flags.convergence = TRUE;
                        PrintLog("Convergence mode is ON.\n");
                    }
                    break;
                case 'C':
                    if(argv[i][2] == '-')
                    {
                        flags.conv_adjoint = FALSE;
                        PrintLog("Convergence of adjoint mode is OFF.\n");
                    }
                    else
                    {
                        flags.conv_adjoint = TRUE;
                        PrintLog("Convergence of adjoint mode is ON.\n");
                    }
                    break;
                case 'a':
                    G = atoi(&argv[i][2]);
                    if(G < 0 || G > 4)
                        warning_msg("Invalid advection code %d.\n", G);
                    else
                        flags.advection = G;
                    PrintLog("Advection code %d\n", flags.advection);
                    break;
                case 's':
                    G = atoi(&argv[i][2]);
                    if(G < 0 || G > 1)
                        warning_msg("Invalid solver code.\n");
                    else
                        flags.solver = G;
                    PrintLog("Solver code %d\n", G);
                    break;
                default:
                    PRINTF("%s: unrecognized option %s\n", argv[0], argv[i]);
            }
        }
    }
    PrintLog("++ done parsing command line.\n");
}

#define MAX_LINE_SIZE       4096

static int parse_input_file(const char *fname)
{
    char line[MAX_LINE_SIZE], *s;
    FILE *F;

    PrintLog("++++ parsing input file %s:\n", fname);
    F = fopen(fname, "r");
    if(F == NULL)
        return FALSE;

    while(!feof(F))
    {
        /* read line from the file */
        memset(line, 0, MAX_LINE_SIZE * sizeof (char));
        s = line;
        while((*s = fgetc(F)) == '\n')
            ;
        if(*s == EOF)
            break;
        while(++s < line + MAX_LINE_SIZE)
        {
            *s = fgetc(F);

            if(*s == ';' || *s == EOF)
                break;
        }

        if(line[0] == '#') /* comment within the file */
            continue;

        s = strtok(line, " \t\n");
        if(strcmp(s, "time") == 0)
        {
            double stop, step;
            s = strtok(NULL, ";");
            step = strtod(s, &s);
            stop = strtod(s, &s);
            SetTimeParams(0.0, stop, step);
            continue;
        }
        if(strcmp(s, "grid") == 0)
        {
            if(grid)
                error_msg("Multiple grids.\n");
            s = strtok(NULL, " \t\n;");
            if(s[0] == '=')
            {
                double X0, X1, Y0, Y1, Z0, Z1;
                int NX, NY, NZ;
                s = strtok(NULL, " \t\n");
                X0 = atof(s);
                s = strtok(NULL, " \t\n");
                X1 = atof(s);
                s = strtok(NULL, " \t\n");
                NX = atoi(s);
                s = strtok(NULL, " \t\n");
                Y0 = atof(s);
                s = strtok(NULL, " \t\n");
                Y1 = atof(s);
                s = strtok(NULL, " \t\n");
                NY = atoi(s);
                s = strtok(NULL, " \t\n");
                Z0 = atof(s);
                s = strtok(NULL, " \t\n");
                Z1 = atof(s);
                s = strtok(NULL, " \t\n;");
                NZ = atoi(s);
                grid = GenerateGrid1(X0, X1, NX, Y0, Y1, NY, Z0, Z1, NZ);
            }
            else if(strcmp(s, "file") == 0)
            {
                char *fname = strtok(NULL, " \t\n;");
                char *ext = strrchr(fname, '.');
                if(ext == NULL || (strcmp(ext, ".g3b") != 0 && strcmp(ext, ".g3t") != 0))
                    load_grid(fname);
                else if(strcmp(ext, "g3b") == 0)
                    grid = LoadGridBin(fname);
                else
                    grid = LoadGrid(fname);
            }
            else
                grid = GenerateGrid(atoi(s));
            continue;
        }
        if(strcmp(s, "heart-geometry") == 0)
        {
            extern unsigned on_ival, on_oval, on_memb, on_pipe, on_chbr;

            unsigned b;
            s = strtok(NULL, " \t\n");
            if(strcmp(s, "file") == 0)
            {
                unsigned codes[50], num;
                char _names[50][50], *names[50];
                for(unsigned i = 0; i < 50; i++) names[i] = _names[i];
                s = strtok(NULL, "; \t\n");
                PrintLog("Reading heart-geometry from file '%s'.\n", s);
                num = parse_patches_infile(s, codes, names, 50, 50);

                for(unsigned i = 0; i < num; i++)
                {
                    if(strcmp(names[i], "in-valve") == 0)
                        on_ival |= codes[i];
                    else if(strcmp(names[i], "out-valve") == 0)
                        on_oval |= codes[i];
                    else if(strcmp(names[i], "membrane") == 0)
                        on_memb |= codes[i];
                    else if(strcmp(names[i], "pipes") == 0)
                        on_pipe |= codes[i];
                    else if(strcmp(names[i], "chamber") == 0)
                        on_chbr |= codes[i];
                    else
                        error_msg("Unknown heart-geometry element '%s'.\n", names[i]);
                }
                continue;
            }
            if(strcmp(s, "in-valve") == 0)
            {
                PrintLog("Assigned %-15s on bdry", "in-valve");
                while((s = strtok(NULL, " \t\n,;")) != NULL)
                {
                    b = atoi(s);
                    if(b >= 8 * sizeof (on_ival))
                        error_msg("%s: bdry code %d is too large\n", __func__, b);
                    PrintLog(" %3u", b);
                    on_ival |= 1 << b;
                }
                PrintLog("\n");
                continue;
            }
            if(strcmp(s, "out-valve") == 0)
            {
                PrintLog("Assigned %-15s on bdry", "out-valve");
                while((s = strtok(NULL, " \t\n,;")) != NULL)
                {
                    b = atoi(s);
                    if(b >= 8 * sizeof (on_oval))
                        error_msg("%s: bdry code %d is too large\n", __func__, b);
                    PrintLog(" %3u", b);
                    on_oval |= 1 << b;
                }
                PrintLog("\n");
                continue;
            }
            if(strcmp(s, "membrane") == 0)
            {
                PrintLog("Assigned %-15s on bdry", "membrane");
                while((s = strtok(NULL, " \t\n,;")) != NULL)
                {
                    b = atoi(s);
                    if(b >= 8 * sizeof (on_memb))
                        error_msg("%s: bdry code %d is too large\n", __func__, b);
                    PrintLog(" %3u", b);
                    on_memb |= 1 << b;
                }
                PrintLog("\n");
                continue;
            }
            if(strcmp(s, "pipes") == 0)
            {
                PrintLog("Assigned %-15s on bdry", "pipes walls");
                while((s = strtok(NULL, " \t\n,;")) != NULL)
                {
                    b = atoi(s);
                    if(b >= 8 * sizeof (on_pipe))
                        error_msg("%s: bdry code %d is too large\n", __func__, b);
                    PrintLog(" %3u", b);
                    on_pipe |= 1 << b;
                }
                PrintLog("\n");
                continue;
            }
            if(strcmp(s, "chamber") == 0)
            {
                PrintLog("Assigned %-15s on bdry", "chamber walls");
                while((s = strtok(NULL, " \t\n,;")) != NULL)
                {
                    b = atoi(s);
                    if(b >= 8 * sizeof (on_chbr))
                        error_msg("%s: bdry code %d is too large\n", __func__, b);
                    PrintLog(" %3u", b);
                    on_chbr |= 1 << b;
                }
                PrintLog("\n");
                continue;
            }
            error_msg("Unknown heart-geometry element '%s'.\n", s);
        }
        if(strcmp(s, "parameter") == 0)
        {
            s = strtok(NULL, " \t\n");
            if(strcmp(s, "reynolds") == 0)
            {
                s = strtok(NULL, ";");
                Re = atof(s);
                PrintLog("Assigned parameter Re = %g\n", (double) Re);
                continue;
            }
            if(strcmp(s, "froude") == 0)
            {
                s = strtok(NULL, ";");
                Fr = atof(s);
                PrintLog("Assigned parameter Fr = %g\n", (double) Fr);
                continue;
            }
            if(strcmp(s, "alpha") == 0)
            {
                s = strtok(NULL, ";");
                alpha = atof(s);
                PrintLog("Assigned parameter alpha = %g\n", (double) alpha);
                continue;
            }
            if(strcmp(s, "beta") == 0)
            {
                s = strtok(NULL, ";");
                beta = atof(s);
                PrintLog("Assigned parameter beta = %g\n", (double) beta);
                continue;
            }
        }
        if(strcmp(s, "flag") == 0)
        {
            s = strtok(NULL, " \t\n");
            if(strcmp(s, "advection") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "rk45") == 0)
                    flags.advection = adv_rk45;
                else if(strcmp(s, "characteristics") == 0)
                    flags.advection = adv_char;
                else if(strcmp(s, "semi-implicit") == 0)
                    flags.advection = adv_semi;
                else if(strcmp(s, "explicit") == 0)
                    flags.advection = adv_explicit;
                else if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0)
                    flags.advection = adv_explicit;
                else if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0)
                    flags.advection = adv_none;
                else
                    warning_msg("flag advection %s' unrecognized "
                        "in file %s\n", s, fname);
                PrintLog("Assigned flag advection = %d\n", flags.advection);
                continue;
            }
            if(strcmp(s, "solver") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "cs") == 0)
                    flags.solver = solver_cs;
                else if(strcmp(s, "pc") == 0)
                    flags.solver = solver_pc;
                else
                    warning_msg("flag solver %s' unrecognized "
                        "in file %s\n", s, fname);
                PrintLog("Assigned flag solver = %d\n", flags.solver);
                continue;
            }
            if(strcmp(s, "stationary") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0 || strcmp(s, "yes") == 0)
                {
                    flags.stationary = 1;
                    PrintLog("Assigned flag stationary = 1\n");
                }
                else if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0 || strcmp(s, "no") == 0)
                {
                    flags.stationary = 0;
                    PrintLog("Assigned flag stationary = 0\n");
                }
                else
                    warning_msg("flag stationary %s' unrecognized "
                        "in file %s\n", s, fname);
                continue;
            }
            if(strcmp(s, "periodic") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0 || strcmp(s, "no") == 0)
                    flags.periodic = per_no;
                else if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0 || strcmp(s, "yes") == 0)
                    flags.periodic = per_default;
                else if(strcmp(s, "march") == 0 || strcmp(s, "marching") == 0)
                    flags.periodic = per_march;
                else if(strcmp(s, "adjoint") == 0)
                    flags.periodic = per_adjoint;
                else if(strcmp(s, "armijo") == 0)
                    flags.periodic = per_armijo;
                else if(strcmp(s, "broyden") == 0)
                    flags.periodic = per_broyden;
                else if(strcmp(s, "lbfgs") == 0)
                    flags.periodic = per_lbfgs;
                else
                    warning_msg("flag periodic %s' unrecognized "
                        "in file %s\n", s, fname);
                PrintLog("Assigned flag periodic = %d (%s)\n", flags.periodic,
                        periodic_str[flags.periodic]);
                continue;
            }
            if(strcmp(s, "max_periods") == 0)
            {
                s = strtok(NULL, ";");
                long maxp = atoi(s);
                MAX_PERIODS = maxp;
                PrintLog("Assigned flag max_periods = %d\n", MAX_PERIODS);
                continue;
            }
            if(strcmp(s, "shape_opt") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0 || strcmp(s, "yes") == 0
                        || strcmp(s, "pit") == 0)
                {
                    flags.shape_opt = opt_pit;
                    PrintLog("Assigned flag shape_opt = %d (pit)\n", flags.shape_opt);
                }
                else if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0 || strcmp(s, "no") == 0)
                {
                    flags.shape_opt = opt_noopt;
                    PrintLog("Assigned flag shape_opt = %d (no opt)\n", flags.shape_opt);
                }
                else if(strcmp(s, "armijo") == 0)
                {
                    flags.shape_opt = opt_armijo;
                    PrintLog("Assigned flag shape_opt = %d (armijo)\n", flags.shape_opt);
                }
                else if(strcmp(s, "custom") == 0)
                {
                    flags.shape_opt = opt_custom;
                    PrintLog("Assigned flag shape_opt = %d (custom)\n", flags.shape_opt);
                }
                else
                    warning_msg("flag shape_opt %s' unrecognized "
                        "in file %s\n", s, fname);
                continue;
            }
            if(strcmp(s, "max_shapes") == 0)
            {
                s = strtok(NULL, ";");
                long maxs = atoi(s);
                MAX_SHAPES = maxs;
                PrintLog("Assigned flag max_shapes = %d\n", MAX_SHAPES);
                continue;
            }
            if(strcmp(s, "need_adjoint") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0 || strcmp(s, "yes") == 0)
                {
                    flags.need_adjoint = 1;
                    PrintLog("Assigned flag need_adjoint = 1\n");
                }
                else if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0 || strcmp(s, "no") == 0)
                {
                    flags.need_adjoint = 0;
                    PrintLog("Assigned flag need_adjoint = 0\n");
                }
                else
                    warning_msg("flag need_adjoint %s' unrecognized "
                        "in file %s\n", s, fname);
                continue;
            }
            if(strcmp(s, "time_order") == 0)
            {
                unsigned o;
                s = strtok(NULL, " \t\n;");
                o = atoi(s);
                if(o > 3)
                    warning_msg("flag time_order %s' invalid "
                        "in file %s\n", s, fname);
                else
                {
                    flags.time_order = o;
                    PrintLog("Assigned flag time_order = %u\n", o);
                }
                continue;
            }
            if(strcmp(s, "transfer") == 0)
            {
                s = strtok(NULL, " \t\n;");
                if(strcmp(s, "interpolation") == 0)
                {
                    flags.transfer = transfer_interpolation;
                    PrintLog("Assigned flag transfer = %u (interpolation)\n",
                            flags.transfer);
                }
                else if(strcmp(s, "taylor") == 0)
                {
                    flags.transfer = transfer_taylor;
                    PrintLog("Assigned flag transfer = %u (taylor)\n",
                            flags.transfer);
                }
                else if(strcmp(s, "none") == 0)
                {
                    flags.transfer = transfer_none;
                    PrintLog("Assigned flag transfer = %u (none)\n",
                            flags.transfer);
                }
                else
                    warning_msg("flag transfer %s' unrecognized "
                        "in file %s\n", s, fname);
                continue;
            }
            if(strcmp(s, "moving_grid") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0 || strcmp(s, "yes") == 0)
                {
                    flags.moving_grid = 1;
                    error_msg("moving_grid is disabled.\n");
                    PrintLog("Assigned flag moving_grid = 1\n");
                    // done_alegrid(alegrid);
                    // alegrid=init_alegrid(grid);
                }
                else if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0 || strcmp(s, "no") == 0)
                {
                    flags.moving_grid = 0;
                    PrintLog("Assigned flag moving_grid = 0\n");
                    // done_alegrid(alegrid);
                    // alegrid=NULL;
                }
                else
                    warning_msg("flag moving_grid %s' unrecognized "
                        "in file %s\n", s, fname);
                continue;
            }
            if(strcmp(s, "use_ale") == 0)
            {
                s = strtok(NULL, " ;");
                if(strcmp(s, "true") == 0 || strcmp(s, "on") == 0 || strcmp(s, "yes") == 0)
                {
                    flags.use_ale = 1;
                    PrintLog("Assigned flag use_ale = 1\n");
                }
                else if(strcmp(s, "false") == 0 || strcmp(s, "off") == 0 || strcmp(s, "no") == 0)
                {
                    flags.use_ale = 0;
                    PrintLog("Assigned flag use_ale = 0\n");
                }
                else
                    warning_msg("flag use_ale %s' unrecognized "
                        "in file %s\n", s, fname);
                continue;
            }
        }
        if(strcmp(s, "function") == 0)
        {
            s = strtok(NULL, " \t\n");
            if(strcmp(s, "exct_vel") == 0)
            {
                s = strtok(NULL, ";");
                /* assigns exact velocity, leaves exact pressure unchanged */
                SetExctStrings(s, NULL);
                PrintLog("Assigned function exct_vel %s\n", s);
                continue;
            }
            if(strcmp(s, "exct_prs") == 0)
            {
                s = strtok(NULL, ";");
                /* assigns exact pressure, leaves exact velocity unchanged */
                SetExctStrings(NULL, s);
                PrintLog("Assigned function exct_prs %s\n", s);
                continue;
            }
            if(strcmp(s, "init_vel") == 0)
            {
                s = strtok(NULL, ";");
                /* assigns initial velocity,  leaves the rest unchanged */
                SetFuncStrings(s, NULL, NULL);
                PrintLog("Assigned function init_vel %s\n", s);
                continue;
            }
            if(strcmp(s, "init_prs") == 0)
            {
                s = strtok(NULL, ";");
                /* assigns initial pressure, leaves the rest unchanged */
                SetFuncStrings(NULL, s, NULL);
                PrintLog("Assigned function init_prs %s\n", s);
                continue;
            }
            if(strcmp(s, "srce_trm") == 0)
            {
                s = strtok(NULL, ";");
                /* assigns source term, leaves the rest unchanged */
                SetFuncStrings(NULL, NULL, s);
                PrintLog("Assigned function srce_trm %s\n", s);
                continue;
            }
            if(strcmp(s, "bdry_vel") == 0)
            {
                int bdry_no;
                unsigned on_bdry;
                s = strtok(NULL, " \t\n");
                if(strcmp(s, "all") == 0)
                    bdry_no = -1;
                else
                    bdry_no = atoi(s);
                if(bdry_no < 0)
                    on_bdry = (unsigned) - 1;
                else
                    on_bdry = 1 << bdry_no;
                s = strtok(NULL, ";");
                SetBdryFuncString(on_bdry, s);
                PrintLog("Assigned boundary condition %d %s\n", bdry_no, s);
                continue;
            }
            if(strcmp(s, "adj_exct_vel") == 0)
            {
                s = strtok(NULL, ";");
                SetAdjExctStrings(s, NULL);
                PrintLog("Assigned function adj_exct_vel %s\n", s);
                continue;
            }
            if(strcmp(s, "adj_exct_prs") == 0)
            {
                s = strtok(NULL, ";");
                SetAdjExctStrings(NULL, s);
                PrintLog("Assigned function adj_exct_prs %s\n", s);
                continue;
            }
            if(strcmp(s, "adj_finl_vel") == 0)
            {
                s = strtok(NULL, ";");
                SetAdjFuncStrings(s, NULL, NULL);
                PrintLog("Assigned function adj_finl_vel %s\n", s);
                continue;
            }
            if(strcmp(s, "adj_finl_prs") == 0)
            {
                s = strtok(NULL, ";");
                SetAdjFuncStrings(NULL, s, NULL);
                PrintLog("Assigned function adj_finl_prs %s\n", s);
                continue;
            }
            if(strcmp(s, "adj_srce_trm") == 0)
            {
                s = strtok(NULL, ";");
                SetAdjFuncStrings(NULL, NULL, s);
                PrintLog("Assigned function adj_srce_trm %s\n", s);
                continue;
            }
            if(strcmp(s, "adj_bdry_vel") == 0)
            {
                int bdry_no;
                unsigned on_bdry;
                s = strtok(NULL, " \t\n");
                if(strcmp(s, "all") == 0)
                    bdry_no = -1;
                else
                    bdry_no = atoi(s);
                if(bdry_no < 0)
                    on_bdry = (unsigned) - 1;
                else
                    on_bdry = 1 << bdry_no;
                s = strtok(NULL, ";");
                SetAdjBdryFuncString(on_bdry, s);
                PrintLog("Assigned adjoint boundary condition %d %s\n", bdry_no, s);
                continue;
            }
            if(strcmp(s, "shape_grad_bdry") == 0)
            {
                int bdry_no;
                unsigned on_bdry;
                s = strtok(NULL, " \t\n");
                if(strcmp(s, "all") == 0)
                    bdry_no = -1;
                else
                    bdry_no = atoi(s);
                if(bdry_no < 0)
                    on_bdry = (unsigned) - 1;
                else
                    on_bdry = 1 << bdry_no;
                s = strtok(NULL, ";");
                SetShGrdBdryFuncString(on_bdry, s);
                PrintLog("Assigned exact shape gradient on boundary %d %s\n", bdry_no, s);
                continue;
            }
            if(strcmp(s, "alebc_ptpos") == 0)
            {
                int bdry_no;
                unsigned on_bdry;
                if(flags.moving_grid == 0)
                {
                    warning_msg("`alebc_ptpos' ignored since "
                            "moving_grid is off.\n");
                    continue;
                }
                s = strtok(NULL, " \t\n");
                if(strcmp(s, "all") == 0)
                    bdry_no = -1;
                else
                    bdry_no = atoi(s);
                if(bdry_no < 0)
                    on_bdry = (unsigned) - 1;
                else
                    on_bdry = 1 << bdry_no;
                s = strtok(NULL, ";");
                // SetALEBCString(alegrid, on_bdry, s);
                PrintLog("Assigned ale boundary position %d %s\n", bdry_no, s);
                continue;
            }
        }
        warning_msg("keyword '%s' in file %s not recognized.\n", s, fname);
    }
    fclose(F);

    s = strrchr(fname, '/');
    if(s == NULL)
        strncpy(log_fname, fname, FILENAME_MAX - 1);
    else
        strncpy(log_fname, ++s, FILENAME_MAX - 1);
    if((s = strrchr(log_fname, '.')) != NULL)
        *s = 0;
    strcat(log_fname, ".log");


    PrintLog("++++ done parsing input file.\n");
    return TRUE;
}

